$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/rewardslog/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '用户ID', name: 'userId', index: 'user_id', width: 80 },
			{ label: '奖励类型', name: 'type', index: 'type', width: 80 ,
                formatter: function(value, options, row){
                    if(value == 1){
                        return "流水分红";
                    }else if(value==2){
                        return "消费分红";
                    }else if(value==3){
                        return "推荐奖励";
                    }else if(value==4){
                        return "团队奖励";
                    }else if(value==5){
                        return "公司奖励";
                    }else if(value==6){
                        return "业绩奖励";
                    }else if(value==7){
                        return "限制佣金下单扣除";
                    }else if(value==8){
                        return "限制佣金下单转化";
                    }
                }
            },
			{ label: '类型描述', name: 'typeDesc', index: 'type_desc', width: 80 }, 			
			{ label: '奖励金额', name: 'amount', index: 'amount', width: 80 }, 			
			{ label: '奖励比例', name: 'ratio', index: 'ratio', width: 80 }, 			
			{ label: '奖励基数', name: 'rewardBase', index: 'reward_base', width: 80 }, 			
			{ label: '贡献者ID', name: 'rewardUserId', index: 'reward_user_id', width: 80 },
			{ label: '贡献者姓名', name: 'rewardUserName', index: 'reward_user_name', width: 80 },
			{ label: '贡献者手机号', name: 'rewardUserMobile', index: 'reward_user_mobile', width: 80 },
			{ label: '贡献者等级', name: 'rewardLevel', index: 'reward_level', width: 80,hidden:true },
			{ label: '贡献者等级名称', name: 'rewardLevelName', index: 'reward_level_name', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $( "#createTime" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate:new Date()
    });
    $( "#createTime1" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate:new Date()
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            userId: null
        },
		showList: true,
		title: null,
		rewardsLog: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.rewardsLog = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.rewardsLog.id == null ? "sys/rewardslog/save" : "sys/rewardslog/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.rewardsLog),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/rewardslog/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/rewardslog/info/"+id, function(r){
                vm.rewardsLog = r.rewardsLog;
            });
		},
        reset:function(){
            vm.q = {userId:null, createTime:null, createTime1:null};
            $("input[name='qInput']").val('');
            vm.reload();

        },
		reload: function (event) {
			vm.showList = true;
            var createTime =   $("#createTime").val();
            var createTime1 = $("#createTime1").val();
            if (createTime.length > 0 && createTime1.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = createTime1.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if(createTime !=null && createTime != ''){
                createTime += " 00:00:00";
            }
            if(createTime1 !=null && createTime1 != ''){
                createTime1 += " 23:59:59";
            }
            parmas=	{'userId':vm.q.userId,'createTime':createTime,'createTime1':createTime1};
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:parmas,
                page:1
            }).trigger("reloadGrid");
		}
	}
});